import {Injectable} from '@angular/core';
import {AndroidPermissions} from "@ionic-native/android-permissions";
import {Diagnostic} from '@ionic-native/diagnostic';
import {Observable} from "rxjs";

@Injectable()
export class PermissionsService {

    constructor(private androidPermissions: AndroidPermissions,
                private diagnostic: Diagnostic) {
    }

    checkPermission(): Promise<any> {
        let a = this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE).then(
            result => {
                console.log('Has permission?', result.hasPermission);
                return result.hasPermission;
            },
            //err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
        );
        let b = this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.RECORD_AUDIO).then(
            result => {
                console.log('Has permission?', result.hasPermission);
                return result.hasPermission;
            },
            //err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.RECORD_AUDIO)
        );
        return Promise.all([a, b]);
    }

    checkAndroidStoragePermission() {
        let a = this.diagnostic.getExternalStorageAuthorizationStatus().then(res => {
            console.log(res);
            return res;
        });

        return a;
    }

    /**
     * 检查安卓打电话的权限
     * @returns {Promise<any>}
     */
    checkAndroidCallPhonePermission(): Promise<any> {
        let a = this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.CALL_PHONE).then(
            res => {
                return res.hasPermission;
            }
        );

        return Promise.all([a]);
    }

    requestPermissions(): Promise<any> {
        return this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE, this.androidPermissions.PERMISSION.RECORD_AUDIO]);
    }

    // 检查android媒体的麦克风权限、存储权限
    checkAndroidAudioPermisson(): Promise<any> {
        // 检查麦克风授权
        let m = this.diagnostic.isMicrophoneAuthorized().then(res => {
            return res;
        });

        // 检查存储授权
        let s = this.diagnostic.isExternalStorageAuthorized().then(res => {
            return res
        });

        return Promise.all([m, s])
    }


    // 检查ios媒体的麦克风权限
    checkIosAudioPermisson(): Promise<any> {
        return this.diagnostic.isMicrophoneAuthorized().then(res => {
            return res
        })
    }

    // 获取android麦克风权限、存储权限
    getAndroidMicPermisson(): Promise<any> {
        let m = this.diagnostic.requestMicrophoneAuthorization().then(res => {
            if (res) {
                this.diagnostic.requestExternalStorageAuthorization().then(res => {
                    return res
                });
            }
        });
        return m;
    }

    // 获取IOS麦克风权限
    getIosMicPermisson(): Promise<any> {
        return this.diagnostic.requestMicrophoneAuthorization().then(res => {
            return res
        })
    }

    //检查android 相册 相机使用权限
    checkCameraAndroidPermission(): Promise<any> {
        // 检查存储授权
        return this.diagnostic.isExternalStorageAuthorized().then(res => {
            return res
        });
    }

    // 检查ios媒体的相机权限
    checkIosCameraPermission(): Promise<any> {
        return this.diagnostic.isCameraAuthorized().then(res => {
            return res
        })
    }


    // 获取android 相册 相机使用权限
    getAndroidCameraPermission(): Promise<any> {
        return this.diagnostic.requestExternalStorageAuthorization().then(res => {
            return res
        });
    }

    // 获取IOS相册 相机使用权限
    getIosCameraPermission(): Promise<any> {
        return this.diagnostic.requestCameraAuthorization().then(res => {
            return res
        })
    }

    //检查通讯录权限
    checkContactPermission(): Promise<any> {
        return this.diagnostic.isContactsAuthorized().then(res => {
            return res;
        });
    }

    // 获取通讯录权限
    getContactsPermission(): Promise<any> {
        return this.diagnostic.requestContactsAuthorization().then(res => {
            return res
        });
    }

    checkLocationPermisson(): Promise<any> {
        let gps = this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION).then(result => {
                console.log('GPS permission?', result.hasPermission);
                return result.hasPermission;
            },
            err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION)
        );

        let wifiLocation = this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION).then(
            result => {
                console.log('wifilocation permission?', result.hasPermission);
                return result.hasPermission;
            },
            err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION)
        );

        return Promise.all([gps, wifiLocation]);
    }

    /*getPermission(){
        console.log(this.diagnostic)
        console.log(this.diagnostic.permissionGroups)
        console.log(this.diagnostic.isMicrophoneAuthorized());
        this.diagnostic.isMicrophoneAuthorized().then(res=>{
            console.log(res)
        })
        this.diagnostic.getMicrophoneAuthorizationStatus().then(res=>{
            alert(res)
            console.log(res);
        })
    }*/

    /**
     * 0-GPS定位权限
     * 1-基站，wifi模糊定位权限
     * @returns {Promise<any>}
     */
    requestLocationPermission(): Promise<any> {
        return this.androidPermissions.requestPermissions(
            [this.androidPermissions.PERMISSION.ACCESS_FINE_LOCATION, this.androidPermissions.PERMISSION.ACCESS_COARSE_LOCATION]
        );
    }

    locationAuthorized(): Promise<any> {

        let isLocationEnabled = this.diagnostic.isLocationEnabled().then(r => {
            return r;
        }).catch(e => {
            return e;
        });

        let isLocationAuthorized = this.diagnostic.isLocationAuthorized().then(r => {
            return r;
        }).catch(e => {
            return e;
        });

        return Promise.all([isLocationEnabled, isLocationAuthorized]);
    }

    /**
     * 申请权限-ios
     * @returns {Promise<any>}
     */
    requestLocationAuthorization(): Promise<any> {
        return this.diagnostic.requestLocationAuthorization().then(r => {
            if (r == 'DENIED_ALWAYS') {
                return false;
            } else {
                return true;
            }
        }).catch(e => {
            return false;
        })
    }

    /**
     * 检测定位服务是否开启
     * @returns {Observable<any>}
     */
    assertLocationService(): Promise<any> {
        return this.diagnostic.isLocationEnabled().then(r => {
            return r;
        }).catch(e => {
            return e;
        });
    }

    /**
     * 检测定位权限是否开启,如果没开启就请求权限
     * @returns {Observable<any>}
     */
    assertLocationAuthorization(): Promise<any> {
        return this.diagnostic.isLocationAuthorized().then(r => {
            return r;
        }).catch(e => {
            return e;
        });
    }

    // 获取android手机所有权限
    getAndroidPermission() {
        this.diagnostic.requestMicrophoneAuthorization().then(res => {
            // this.diagnostic.requestExternalStorageAuthorization().then(res=>{
            this.diagnostic.requestLocationAuthorization().then(res => {
                // this.diagnostic.requestRuntimePermission(this.androidPermissions.PERMISSION.CALL_PHONE);
                this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.CALL_PHONE]).then(res => {
                    this.diagnostic.requestCameraAuthorization().then(res => {
                        this.diagnostic.requestContactsAuthorization().then(res => {
                        });
                    });
                })
            })
            // });
        });
    }

    // 获取ios手机所有权限
    getIosPermission() {
        this.diagnostic.requestMicrophoneAuthorization().then(res => {
            this.diagnostic.requestLocationAuthorization().then((res) => {
                this.diagnostic.requestCameraAuthorization().then(res => {
                    this.diagnostic.requestContactsAuthorization().then(res => {
                    });
                });
            })
        });
    }
}
